<template>
	<view>
		<view style="background-color: #FFFFFF;height: 100% !important;">
			<view class="context">
				<view style="background-color: #FFFFFF;height: 500upx;text-align: center;padding-top: 100upx;">
					<image lazy-load="true" :src="logoUrl" style="width: 294upx;height: 294upx;border-radius: 50%;">
					</image>
				</view>

				<view style="text-align: center;margin-top: 100upx;">
					<button style="width: 400upx;" @tap="toGetUserInfo" class="cf-bgcolorTheme">微信一键登录</button>
				</view>
			</view>

		</view>

		<ayAd :type="2" :showOffAt="true"></ayAd>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import ayAd from '@/components/ay-ad/ay-ad.vue';
	import conf from '@/api/conf.js'
	export default {
		components: {
			ayAd,
		},
		computed: {
			...mapState(['hasLogin'])
		},
		data() {
			return {
				themeColor: '#33CCCC',
				logoUrl: 'https://cdn.pixabay.com/photo/2016/11/23/17/55/beach-1854072__340.jpg',
			}
		},
		onLoad() {
			let that = this;

		},
		// #ifdef MP-WEIXIN
		//微信小程序的分享
		onShareAppMessage: function(options) {

		},
		// #endif
		methods: {
			toGetUserInfo(e) {
				let that = this;
				let userInfo = {};
				uni.getUserProfile({
					desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: (res) => {
						userInfo = res.userInfo,
							console.log(res)
						uni.login({
							provider: 'weixin',
							success: function(loginRes) {
								console.log(loginRes);
								//界面不能直接调起微信的openId
								let data = {
									code: loginRes.code,
									AppID_wx: conf.AppID_wx,
									AppSecret_wx: conf.AppSecret_wx,
									pre_url_wx: conf.pre_url_wx,
								};
								uniCloud.callFunction({
									name: 'z-code2Session',
									data: data,
								}).then((res) => {
									console.log(res)
									if (res.result.code === 0) {
										let openId = res.result.data.openid;
										userInfo['openId'] = openId;
										console.log('用户服务商唯一用户标识openId为：' + userInfo
											.openId);
										that.userInfo = userInfo;
										that.$store.dispatch("loginFun", userInfo);
										//保存登录信息至缓存
										let toPage = setTimeout(function() {
											uni.navigateBack({

											})
											clearTimeout(toPage);
										}, 500)

										//账号登录的状态
										try {
											uni.setStorageSync('isLogin', true);
										} catch (e) {
											// error
										}
									}

								})

							},
							fail: function(err) {
								console.log(err);
								return uni.showToast({
									title: '微信登录失败',
									duration: 2000,
									icon: 'none'
								})
							}
						});

					}
				})

			}


		},

	}
</script>

<style lang="scss">
</style>
